/*
 * EmployeesPanel.java
 *
 * Created on 29 marzec 2007, 00:32
 */

package client;

import data.*;
import java.rmi.RemoteException;
import java.util.Set;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;

/**
 *
 * @author  Przemek
 */
public class EmployeesPanel extends javax.swing.JPanel
{
    Set<Employee> employees;
    Employee editedEmp;
    
    /** Creates new form EmployeesPanel */
    public EmployeesPanel()
    {
        initComponents();
        fillTeamsComboBox();
        fillEmployeesList();
    }
    
    private void fillTeamsComboBox()
    {
        try
        {
            Set<Team> teams = VacationClient.getInstance().getRemoteServer().getTeams();
            jComboBoxTeam.setModel( new DefaultComboBoxModel( teams.toArray() ));
        } catch (RemoteException ex)
        {
            VacationClient.printException( ex, this );
        }
        

        
    }
    
    private void fillEmployeesList()
    {
        try
        {
            employees = VacationClient.getInstance().getRemoteServer().getEmployees();
            jListEmployees.setListData( employees.toArray() );
        }
        catch (RemoteException ex)
        {
            VacationClient.printException( ex, this );
        }
        
        fillEmployeeDetails( null );
    }
    
    private void fillEmployeeDetails( Employee e )
    {
        editedEmp = e;
        
        if( e == null )
        {
            jTextFieldName.setText( null );
            jComboBoxTeam.setSelectedItem( null );
        }
        else
        {
            jTextFieldName.setText( e.getName() );
            jComboBoxTeam.setSelectedItem( e.getTeam() );
        }
        
        jButtonRemove.setEnabled( e != null );
        jButtonSet.setEnabled( e != null );        
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents()
    {
        jScrollPane1 = new javax.swing.JScrollPane();
        jListEmployees = new javax.swing.JList();
        jLabel1 = new javax.swing.JLabel();
        jTextFieldName = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jComboBoxTeam = new javax.swing.JComboBox();
        jButtonNew = new javax.swing.JButton();
        jButtonRemove = new javax.swing.JButton();
        jButtonSet = new javax.swing.JButton();

        setLayout(null);

        setMinimumSize(new java.awt.Dimension(640, 480));
        jListEmployees.addListSelectionListener(new javax.swing.event.ListSelectionListener()
        {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt)
            {
                jListEmployeesValueChanged(evt);
            }
        });

        jScrollPane1.setViewportView(jListEmployees);

        add(jScrollPane1);
        jScrollPane1.setBounds(20, 10, 170, 410);

        jLabel1.setText("Name");
        add(jLabel1);
        jLabel1.setBounds(225, 42, 40, 14);

        add(jTextFieldName);
        jTextFieldName.setBounds(280, 40, 220, 20);

        jLabel2.setText("Team");
        add(jLabel2);
        jLabel2.setBounds(225, 102, 40, 14);

        add(jComboBoxTeam);
        jComboBoxTeam.setBounds(280, 100, 220, 22);

        jButtonNew.setText("New");
        jButtonNew.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                jButtonNewActionPerformed(evt);
            }
        });

        add(jButtonNew);
        jButtonNew.setBounds(230, 170, 80, 23);

        jButtonRemove.setText("Remove");
        jButtonRemove.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                jButtonRemoveActionPerformed(evt);
            }
        });

        add(jButtonRemove);
        jButtonRemove.setBounds(340, 170, 80, 23);

        jButtonSet.setText("Set");
        jButtonSet.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                jButtonSetActionPerformed(evt);
            }
        });

        add(jButtonSet);
        jButtonSet.setBounds(450, 170, 80, 23);

    }// </editor-fold>//GEN-END:initComponents

    private void jButtonSetActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jButtonSetActionPerformed
    {//GEN-HEADEREND:event_jButtonSetActionPerformed
        editedEmp.setName( jTextFieldName.getText() );
        editedEmp.setTeam( (Team) jComboBoxTeam.getSelectedItem() );
        
        try
        {            
            Team newTeam = VacationClient.getInstance().getRemoteServer().reloadTeam( editedEmp.getTeam(), true, false );
            newTeam.getMembers().add( editedEmp );
            newTeam = VacationClient.getInstance().getRemoteServer().saveTeam( newTeam );
        }
        catch (RemoteException ex)
        {
            VacationClient.printException( ex, this );
        }
        
        jListEmployees.repaint();        
    }//GEN-LAST:event_jButtonSetActionPerformed

    private void jListEmployeesValueChanged(javax.swing.event.ListSelectionEvent evt)//GEN-FIRST:event_jListEmployeesValueChanged
    {//GEN-HEADEREND:event_jListEmployeesValueChanged
        fillEmployeeDetails( (Employee) jListEmployees.getSelectedValue() );
    }//GEN-LAST:event_jListEmployeesValueChanged

    private void jButtonRemoveActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jButtonRemoveActionPerformed
    {//GEN-HEADEREND:event_jButtonRemoveActionPerformed
        try
        {
            VacationClient.getInstance().getRemoteServer().delete( editedEmp );
            employees.remove( editedEmp );
            editedEmp = null;
            jListEmployees.setListData( employees.toArray() );
        }
        catch (RemoteException ex)
        {
            VacationClient.printException( ex, this );
        }                
    }//GEN-LAST:event_jButtonRemoveActionPerformed

    private void jButtonNewActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jButtonNewActionPerformed
    {//GEN-HEADEREND:event_jButtonNewActionPerformed
        if( jComboBoxTeam.getSelectedItem() == null )
        {
            JOptionPane.showMessageDialog( this, "You must choose team for employee", "Error", JOptionPane.ERROR_MESSAGE );
            return;
        }
        
        Employee newEmp = new Employee();
        
        newEmp.setName( jTextFieldName.getText() );
        newEmp.setTeam( (Team) jComboBoxTeam.getSelectedItem() );
        
        try
        {            
            Team team = VacationClient.getInstance().getRemoteServer().reloadTeam( newEmp.getTeam(), true, false );
            team.getMembers().add( newEmp );
            team = VacationClient.getInstance().getRemoteServer().saveTeam( team );
        }
        catch (RemoteException ex)
        {
            VacationClient.printException( ex, this );
        }
        
        employees.add( newEmp );
        jListEmployees.setListData( employees.toArray() );
    }//GEN-LAST:event_jButtonNewActionPerformed
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonNew;
    private javax.swing.JButton jButtonRemove;
    private javax.swing.JButton jButtonSet;
    private javax.swing.JComboBox jComboBoxTeam;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JList jListEmployees;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField jTextFieldName;
    // End of variables declaration//GEN-END:variables
    
}
